package plantshop.server;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import plantshop.model.Client;



public class ServerCommunicateThread extends Thread implements Serializable{
	private static final long serialVersionUID = -6127526339691388201L;
	private List<Client> clients;
	private boolean gameInProgress;

	public ServerCommunicateThread(List<Client> clients){
		this.clients = clients;
		gameInProgress = true;
	}
	@Override
	public void run() {
		try{
			for(Client client: clients){
				client.setGender(Boolean.parseBoolean(client.read()));
				client.setName(client.read());
			}

			for(Client client:clients){
				for(Client client2: clients){
					if(client != client2){
						client.write(client2.getGender());
						client.write(client2.getName());
					}
				}
				client.write("Begin Game");
			}

			//Works up to here
			//Add functionality for sending data back and forth during game
			while(gameInProgress){
				for(Client client: clients){
					client.setScore(Integer.parseInt(client.read()));
					client.setMoney(Integer.parseInt(client.read()));
				}
				for(Client client:clients){
					for(Client client2: clients){
						if(client != client2){
							client.write(client2.getPlayerScore());
							client.write(client2.getPlayerMoney());
						}
					}
				}
				Thread.sleep(100);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e){
			e.printStackTrace();
		}
		
	}

	public void endgame(){
		gameInProgress = false;
	}
}
